package com.tg.app.activity.device.add;

import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tange.module.core.wifi.scan.ApScanConfiguration;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.adapter.ApHistryDeviceAdapter;
import com.tg.app.helper.ActivityHelper;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;
import com.tg.data.http.entity.BindHistryBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public  class ApBindHistroyDeviceListActivity extends BaseActivity {
    public static final  String TAG ="ApBindHistroyDeviceListActivity";
    private RecyclerView recyclerView;
    private ApHistryDeviceAdapter apDeviceAdapter;
    private final List<BindHistryBean> wifiApList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.fragment_ap_device_histroy_list);
        hideActionBar();
        initView();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        getWifiList();
    }


    @Override
    protected void initView() {
        modifyToolBar(R.string.text_my_histroy_device);
        recyclerView = findViewById(R.id.device_add_ap_camera_list);
        LinearLayoutManager linearLayoutManager =   new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        apDeviceAdapter = new ApHistryDeviceAdapter(wifiApList, position -> {
            AddDeviceReportHelper.onEventClickByName(TAG,"item_" + position);
            if (wifiApList.size()>position){
                BindHistryBean ap = wifiApList.get(position);
                String type = ap.getDevice_type();
                if (!StringUtils.isEmpty(type) && (type.contains("sim") || !type.contains("dr"))){//4G
                    String uuid = ap.getUuid();
                    if (!StringUtils.isEmpty(uuid) && uuid.startsWith(ApScanConfiguration.INSTANCE.apNamePrefixEndsWithUnderLine())){
                        uuid = uuid.replace(ApScanConfiguration.INSTANCE.apNamePrefixEndsWithUnderLine(), "");
                    }
                    ActivityHelper.goToBindActivity(ApBindHistroyDeviceListActivity.this, uuid, ScanQrcodeActivity.EXT_FROM_HISTROY);
                }
            }
        });
        recyclerView.setAdapter(apDeviceAdapter);
        setClickBack(v -> {
            AddDeviceReportHelper.onEventClickByName(TAG,"back_toolbar");
            finish();
        });
    }

    private void getWifiList(){
        TGLog.d("getWifiList");
        HashMap<String, String> map = new HashMap<>();
        TGHttp.getInstance().getBindHistroy(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<List<BindHistryBean>>() {
                    @Override
                    protected void onSuccess(final List<BindHistryBean> items) {
                        TGLog.d( "content = " +  items.size());
                        if (items.size() > 0) {
                            for (BindHistryBean bindHistryBean : items){
                                TGLog.d("device_type = " + bindHistryBean.getDevice_type());
                                if (!bindHistryBean.getUuid().startsWith(ApScanConfiguration.INSTANCE.apNamePrefixEndsWithUnderLine())){
                                    bindHistryBean.setUuid(ApScanConfiguration.INSTANCE.apNamePrefixEndsWithUnderLine() + bindHistryBean.getUuid());
                                }
                            }
                            wifiApList.clear();
                            wifiApList.addAll(items);
                            if (wifiApList.size()>0){
                                findViewById(R.id.ll_1).setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);
                            }else  {
                                recyclerView.setVisibility(View.GONE);
                                findViewById(R.id.ll_1).setVisibility(View.VISIBLE);
                            }
                            apDeviceAdapter.notifyDataSetChanged();
                        }else{
                            recyclerView.setVisibility(View.GONE);
                            findViewById(R.id.ll_1).setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        TGLog.d("errorInfo = " + errorInfo);
                        recyclerView.setVisibility(View.GONE);
                        findViewById(R.id.ll_1).setVisibility(View.VISIBLE);
                    }

                    @Override
                    protected void onOtherError(String error) {
                        TGLog.d("error = " + error);
                        recyclerView.setVisibility(View.GONE);
                        findViewById(R.id.ll_1).setVisibility(View.VISIBLE);
                    }
                });
    }
}
